#ifndef PARSETRAINDATA_H
#define PARSETRAINDATA_H

#include <QtCore>
#include <QtGui>
#include <QtNetwork>

struct FirmTrainStruct {
	QString Number1, Number2;
	QString Name;
};

typedef QList<FirmTrainStruct> FirmTrainsList;

struct CarDataStruct {
	QString Number;
	QString UpperPlaces;
	QString LowerPlaces;
	QString UpperSidePlaces;
	QString LowerSidePlaces;
	QString OtherPlaces;
};

typedef QList<CarDataStruct> CarDataList;

struct AdvClassDesc {
	QString Class;
	QString Service;
	QString Category;
};

struct ClassesStruct {
	QString Class;
	QString Category;
	QString Price;
	AdvClassDesc AdvDesc;
	CarDataList Car;
};

typedef QList<ClassesStruct> ClassesList;

struct TrainDataStruct {
// ------ mainpage
	QString Name, NameUrl;
	int Id;
	QString Path, PathUrl;
	QString Start;
	QString End;
	QString Delta;
// ----- subpage
	ClassesList Class;
};

typedef QList<TrainDataStruct> TrainDataList;

class ParseTrainData: public QObject {
	Q_OBJECT
public:
	ParseTrainData(QObject * Parent = 0);
	~ParseTrainData();
	void SetFirmTrainsList(FirmTrainsList * FirmTrains);
	static const char * Host();
	QTableWidget * Table();
	const TrainDataList & SessionData();
	void SetCollapsable(bool State);
	void SortBy(int Col);
	enum {
		COL_NAME = 0,
		COL_PATH,
		COL_START,
		COL_END,
		COL_DELTA,
		COL_CLASS,
		COL_CATEGORY,
		COL_PRICE,
		COL_NUMBER,
		COL_UPPERPLACES,
		COL_LOWERPLACES,
		COL_UPPERSIDEPLACES,
		COL_LOWERSIDEPLACES,
		COL_OTHERPLACES,
		COLS_COUNT};
public slots:
	void Exec(const QString & From, const QString & To, const QString & Date);
	void Print(const TrainDataList & List);
	void Cancel();
private:
	QHttp * m_Http;
	QList<QHttp *> m_SubHttp;
	int m_SubPagesCount, m_SubPagesFinished;
	TrainDataList m_SessionData;
	QTableWidget * m_Table;
	FirmTrainsList * m_FirmTrains;
	bool m_Collapsable;
	void ParseMainPage(const QString & Response);
	void ParseSubPage(const QString & Response, int Id);
	void CreateAdvClassDesc(ClassesStruct & Class);
	void CreateTableHeaders();
private slots:
	void OnMainPageRequestFinished(int Id, bool _Error);
	void OnSubPageRequestFinished(int Id, bool _Error);
	void OnSort(int Index);
signals:
	void MainPageFinished(TrainDataList List);
	void SubPagesFinished(TrainDataList List);
	void SubPagesProcessing(TrainDataList List);
	void Error(QString Text);
	void ProgressUpdate(int Done, int Total);
};

#endif // PARSETRAINDATA_H
